<?php defined('BASEPATH') or exit('No direct script access allowed');

class Showmanage_m extends MY_Model {
	
	function __construct(){
		parent::__construct();
		$this->load->library('module_helper');
	}
	
	function view_edit(){
		$SHOW = $this->show_entity->init('id', intval($_GET['show_id']));
		return $SHOW;
	}

	function bid(){
		$SHOW = $this->show_entity->init('id', intval($_GET['show_id']));
		$bid = $this->db->where('show_ref_no', $SHOW->getRefNo())->order_by('id','desc')->get(TBL_BID)->result();
		return array('show'=>$SHOW, 'bid'=>$bid);
	}
	
	function bid_details(){
		$SHOW = $this->show_entity->init('id', intval($_GET['show_id']));
		$BID = $this->bid_entity->init('id', intval($_GET['bid_id']));
		
		if($BID->getShowRefNo() != $SHOW->getRefNo()){
			show_404();exit();
		}
		$booking_records = $this->db->where( array('account_code'=>$BID->getAccountCode(), 'show_ref_no'=>$BID->getShowRefNo()) )->get(TBL_BOOKING_FOR)->result();
		return array('show'=>$SHOW, 'bid'=> $BID, 'booking'=>$booking_records);
	}
	
	function accept_bid(){
		$SHOW = $this->show_entity->init('id', intval($_GET['show_id']));
		$BID = $this->bid_entity->init('id', intval($_GET['bid_id']));
		
		if($BID->getShowRefNo() != $SHOW->getRefNo()){
			show_404();exit();
		}
		$USER = $this->user_entity->init('acc_code', $BID->getAccountCode());
		
		$SHOW->setUserBidWon($USER->getId());
		$SHOW->setStatus('closed');
		$SHOW->update();
		//send email here
		$this->crontab_email->cronUpdate("BID-ACCEPT-BY-SELLER_".$SHOW->getRefNo(),'_sendEmailToUser_sellerAcceptBid', strval( $SHOW->getRefNo().",".$BID->getId() ), array(time()) );
		
		$this->crontab_email->cronUpdate("NOTIFY_BUYER_PAY_".$SHOW->getRefNo(),'_sendEmailToUser_notifyBuyerPayEscrow', strval( $SHOW->getRefNo().",".$BID->getId() ),
											array( time() ) );
		$this->crontab_email->cronUpdate("REMIND_BUYER_PAY_".$SHOW->getRefNo(),'_sendEmailToUser_remindBuyerPayEscrow', strval( $SHOW->getRefNo().",".$BID->getId() ),
											array( workingDaysTimeStamp(6), workingDaysTimeStamp(4), workingDaysTimeStamp(5) ) );		
		$this->crontab_email->cronUpdate("NOTIFY_SELLER_ESCROW_RECEIPT".$SHOW->getRefNo(),'_sendEmailToUser_notifySellerEscrowReceipt', strval( $SHOW->getRefNo() ),
											array( workingDaysTimeStamp(7) ) );
		$this->crontab_email->cronUpdate("NOTIFY_ADMIN_SEND_EMAIL_2_BANK".$SHOW->getRefNo(),'_sendEmailToAdmin_notifySendEmailToBank', strval( $SHOW->getRefNo() ), array(time()) );
											
		$this->crontab_email->cronUpdate("NOTIFY_ADMIN_SEND_EMAIL_2_SELLER_1".$SHOW->getRefNo(),'_sendEmailToAdmin_notifySendEmailToSeller1', strval( $SHOW->getRefNo() ), array(workingDaysTimeStamp(5)) );
		
		$this->crontab_email->cronUpdate("NOTIFY_ADMIN_SEND_EMAIL_RELEASE".$SHOW->getRefNo(),'_sendEmailToAdmin_notifyReleaseContact', strval( $SHOW->getRefNo() ), array(workingDaysTimeStamp(6)) );
		
		$this->crontab_email->cronUpdate("NOTIFYSELLER_ESCROWRELEASE".$SHOW->getRefNo(),'_sendEmailToSeller_escrowReleaseNotify', strval( $SHOW->getId().",".$BID->getId() ), array(workingDaysTimeStamp(15)) );
		
		$this->crontab_email->cronUpdate("NOTIFY_ADMIN_SEND_EMAIL_BANK_RELEASE_FUND".$SHOW->getRefNo(),'_sendEmailToAdmin_notifyAdminSendEmailToBankReleaseFund', strval( $SHOW->getRefNo() ), array(workingDaysTimeStamp(15)) );
			
		$this->crontab_email->cronUpdate("SEND_EMAIL_NOTIFY_FILE_DISPUTE".$SHOW->getRefNo(),'_sendEmailToAll_notifySellerOrBuyerFileDispute', strval( $SHOW->getRefNo() ), array(workingDaysTimeStamp(17)) );
		
		$this->crontab_email->cronUpdate("REMIND_ADMIN_SEND_MAIL_2_BANK_RELEASE_FUND".$SHOW->getRefNo(),'_sendEmailToAdmin_remindAdminSendEmailToBankReleaseFund', strval( $SHOW->getRefNo() ), array(workingDaysTimeStamp(17)) );
		
		/*
		$this->crontab_email->cronUpdate( "NOTIFYBUYER_ESCROWSECOND",'_sendEmailToBuyer_escrowPaymentNotify', strval( $SHOW->getRefNo().",".$BID->getId().",".workingDaysTimeStamp2(35, time()) ) , array(workingDaysTimeStamp2(30, time())) );
		$this->crontab_email->cronUpdate("REMIND_BUYER_PAY_SECOND_".$SHOW->getRefNo(),'_sendEmailToBUYER_remindBuyerPaySecondEscrow', strval( $SHOW->getRefNo().",".$BID->getId().",".workingDaysTimeStamp2(35, time()) ),
											array( workingDaysTimeStamp2(33,time()), workingDaysTimeStamp2(34,time()), workingDaysTimeStamp2(35,time()) ) );
		*/	
		
		echo 'ok';
		exit();
	}
	
	function show_details(){
		$show_ref = $this->uri->segment(3,'');
		$SHOW = $this->show_entity->init('ref_no', $show_ref);
		
		if(!$SHOW){
			show_404();exit();
		}
		return $SHOW;
	}
	
	
}